﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Authentication;
using System.Security.Principal;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using Wicresoft.EVM.Reporting.RSService;
using Wicresoft.Logger;

namespace Wicresoft.EVM.Reporting
{
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            ReportViewer current = 0;

            var r = HttpContext.Current.Request.QueryString["r"];
            if (r != null)
                current = (ReportViewer)int.Parse(r);
            else
                current = Util.Reports.Keys.FirstOrDefault(p => (p & Util.LogonUser.Role) != 0);

            if ((current & Util.LogonUser.Role) == 0)
            {
                Log.Error("Report", "User {0} with role {1} doesn't have sufficient permission to view {2} page!", Util.LogonUser.Name, (int)Util.LogonUser.Role, current);
                this.Response.Redirect(Util.AccessDeniedUrl);
            }

            ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
            IReportServerCredentials irsc = new CustomReportCredentials(Util.ReportingUserName, Util.ReportingUserPSW, Util.ReportingUserDomain);


            ReportViewer1.ServerReport.ReportServerCredentials = irsc;
            ReportViewer1.ServerReport.ReportServerUrl = new Uri(Util.ReportServerUrl);
            ReportViewer1.ServerReport.ReportPath = Util.Reports[current][1];

            Title = Util.Reports[current][0];
        }
    }


}
